package com.example.roamio.controller;

import com.example.roamio.dto.EmailPasswordDTO;
import com.example.roamio.common.Result;
import com.example.roamio.dto.EmailRegisterDTO;
import com.example.roamio.dto.UpdateUsernameDTO;
import com.example.roamio.interceptor.LoginInterceptor;
import com.example.roamio.service.AuthService;
import com.example.roamio.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final AuthService authService;
    private final UserService userService; // ✅ 添加这一行

    @PostMapping("/send-code")
    public Result<String> sendCode(@RequestParam String email) {
        return authService.sendCode(email);
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody EmailRegisterDTO dto) {
        System.out.println("收到注册请求: " + dto);
        return authService.register(dto);
    }

    @PostMapping("/login-by-password")
    public Result<String> loginByPassword(@RequestBody EmailPasswordDTO dto) {
        return authService.loginByPassword(dto);
    }

    @PostMapping("/update-username")
    public Result<String> updateUsername(@RequestBody UpdateUsernameDTO dto) {
        Integer userId = LoginInterceptor.CURRENT_USER_ID.get(); // 从 ThreadLocal 获取当前用户 ID
        return userService.updateUsername(userId, dto.getUsername());
    }
}
